const db = require('../db/index')
const bcrypt = require('bcryptjs')
const jwt = require("jsonwebtoken")
const config = require("../config.js")

exports.getLoginLog = (req, res) => {
    let pageNum = req.body.pageNum;
    let pageSize = req.body.pageSize;
    const sql = "select * from login_log order by login_time desc" + ` limit ${(pageNum - 1) * pageSize} ,${pageSize}`;
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        const sql1 = "select count(*) as total from login_log order by login_time desc";
        db.query(sql1, (err, results1) => {
            // 执行 SQL 语句失败
            if (err) return res.cc(err);
            res.send({
                // 状态
                status: 0,
                // 状态描述，判断 err 是 错误对象 还是 字符串
                message: '查询成功',
                data: {
                    list: results,
                    total: results1[0].total
                }
            })
        })
    })
}

exports.addLoginLog = (req, res) => {
    let login_time = new Date().toLocaleString();
    let data = {
        account: req.body.account,
        name: req.body.name,
        email: req.body.email,
        login_time
    }
    let sql = "insert into login_log set ?";
    db.query(sql, data, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        if (results.affectedRows !== 1) {
            return res.send({ status: 1, message: '登录日志记录失败' })
        }
        res.send({ status: 0, message: '登录日志记录成功！' })
    })
}

exports.deleteLoginLog = (req, res) => {
    let sql = "truncate table login_log ";
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        res.send({ status: 0, message: '清空登录日志成功！' })
    })
}



exports.getOperationLog = (req, res) => {
    let pageNum = req.body.pageNum;
    let pageSize = req.body.pageSize;
    const sql = "select * from operation_log order by operation_time desc" + ` limit ${(pageNum - 1) * pageSize} ,${pageSize}`;
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        const sql1 = "select count(*) as total from operation_log order by operation_time desc";
        db.query(sql1, (err, results1) => {
            // 执行 SQL 语句失败
            if (err) return res.cc(err);
            res.send({
                // 状态
                status: 0,
                // 状态描述，判断 err 是 错误对象 还是 字符串
                message: '查询成功',
                data: {
                    list: results,
                    total: results1[0].total
                }
            })
        })
    })
}

exports.addOperationLog = (req, res) => {
    let operation_time = new Date().toLocaleString();
    let data = {
        operation_person: req.body.operation_person,
        operation_content: req.body.operation_content,
        operation_level: req.body.operation_level,
        operation_time
    }
    let sql = "insert into operation_log set ?";
    db.query(sql, data, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        if (results.affectedRows !== 1) {
            return res.send({ status: 1, message: '操作日志记录失败' })
        }
        res.send({ status: 0, message: '操作日志记录成功！' })
    })
}

exports.deleteOperationLog = (req, res) => {
    let sql = "truncate table operation_log ";
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        res.send({ status: 0, message: '清空操作日志成功！' })
    })
}
